# cocoa bridge
from Foundation import *
from AppKit import *


def log(*a):
	NSLog(" ".join([str(x) for x in a]))


class threadedAction(object):
	"""
	Simple class for making an action run in a separate thread
	"""
	def __init__(self, function, onCompletion = None, *args):
		self.function = function
		self.args = args
		self.onCompletion = onCompletion

	def run(self):
		NSThread.detachNewThreadSelector_toTarget_withObject_("run:", self, None)

	def run_(self, sender):
		pool = NSAutoreleasePool.alloc().init()
		log("calling function ", self.function, " with args: ", self.args)
		res = self.function(*self.args)
		if not self.onCompletion is None:
			log("calling function ", self.onCompletion, " with args: ", res)
			self.onCompletion(res)
		log("done.")
		del pool